<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn">点我点我</button>
    <script>
        //移除监听后，闭包引用断开，bigData会被 GC回收
        /*   function registerHandler() {
              const bigData = new Array(1000000).fill('!') //无法垃圾回收
              //手动将bigData设置为null
              function handler() {
                  console.log(bigData.length)
              }
              const btn = document.getElementById('btn')
              btn.addEventListener('click', handler)
              setTimeout(()=>{
                  btn.removeEventListener('click', handler)
              },3000)
              
          } */
        registerHandler()

        //手动断开引用
        function registerHandler() {
            let bigData = new Array(10000).fill('?')
            document.getElementById('btn').addEventListener('click', () => {
                console.log(bigData.length)
                bigData = null //手动触发垃圾回收
            })

        }
    </script>
</body>

</html>